// pages/home/home.js
import {getChannels,getArticles} from '../../api/home'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text_flushed:'',
    homeTitleData:[],
    show:false,
    showTab:false,
    refresher:true,
    Content:{
      // 频道
      channel_id:0,
      // 时间戳
      timestamp : + new Date(),
      // 是否包含置顶
      with_top : 1,
    },
    listContent:[],
    oldTimestamp:''
  },

  godetail(e) {
    console.log(e)
    wx.navigateTo({
      url:'/pages/detail/detail?id=' + e.currentTarget.dataset.id
    })
  },

  showFrame() {
    this.setData({
      show:true
    })
    wx.hideTabBar()
  },

  onClose() {
    this.setData({
      show:false
    })
    wx.showTabBar()
  },

  downFlushed() {
    this.setData({
      text_flushed:'下拉即可刷新...'
    })
  },

  Refreshing() {
    this.getListContent()
    this.setData({
      listContent:[],
      text_flushed:'正在刷新...',
    })
  },

  // 触底
  hitBottom() {
    console.log(123);
    this.setData({
      "Content.timestamp" : this.data.oldTimestamp
    })
    this.getListContent(this.data.Content)
  },

  checkTab(e) {
    console.log(e);
    this.setData({
      listContent:[],
      "Content.channel_id":e.detail.index,
    })
    this.getListContent(this.data.tabId)
  },

  async getListContent() {
    const {data} = await getArticles(this.data.Content)
    console.log(data);
    this.setData({
      oldTimestamp : data.pre_timestamp,
      listContent:[...this.data.listContent , ...data.results],
      text_flushed:'刷新完成',
      refresher : false
    })
  },

  async getHomeData() {
    const {data:{channels}} = await getChannels()
    console.log(channels);
    this.setData({
      homeTitleData : channels,
      showTab : true
    })
    console.log(this.data.homeTitleData);
  },

  search(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

  onPulling() {
    setTimeout(() => {
      this.setData({
        refresher : false
      })
    },1000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getHomeData()
    this.getListContent()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})